package com.young.common.client.log.core;

import com.young.common.client.log.configuration.MonitorLogConfiguration;
import com.young.common.client.log.handler.BasicLogHandler;
import com.young.common.exception.BaseException;
import com.young.common.util.StringUtils;
import com.young.interfaces.log.service.ILogHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 日志处理器工厂
 * 提供自定义日志处理器注册服务,并根据注册名称产出对应的日志处理器实例
 * Created by rookie on 2017/11/10.
 */
@Configuration
@ConditionalOnBean({MonitorLogConfiguration.class})
public class LogHandlerFactory implements ApplicationContextAware {

    private static final Logger logger = LoggerFactory.getLogger(LogHandlerFactory.class);
    /**
     * 日志处理器池
     */
    private static Map<String, ILogHandler> handlerPool = new HashMap<String, ILogHandler>();
    /**
     * 默认处理器
     */
    private static ILogHandler defaultHandler;

    @Autowired
    MonitorLogConfiguration monitorLogConfiguration;

    /**
     * 根据处理器名称获取处理器
     * @param handlerName
     * @return
     */
    public static ILogHandler getHandler(String handlerName){
        return handlerPool.get(handlerName);
    }

    public static Map<String, ILogHandler> getHandlerPool() {
        return handlerPool;
    }

    public static void setHandlerPool(Map<String, ILogHandler> handlerPool) {
        LogHandlerFactory.handlerPool = handlerPool;
    }

    public static ILogHandler getDefaultHandler() {
        return defaultHandler;
    }

    public static void setDefaultHandler(ILogHandler defaultHandler) {
        LogHandlerFactory.defaultHandler = defaultHandler;
    }

    //初始化
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        logger.info("[日志处理器工厂] 初始化开始,配置参数:{}", monitorLogConfiguration);

        if (monitorLogConfiguration == null) {
            logger.error("[日志处理器工厂] 初始化失败:配置参数无效,请检查young.log.client.monitor-log下的有关配置.");
            throw new BaseException("日志处理工厂初始化失败:配置参数无效,请检查young.log.client.monitor-log下的有关配置.");
        }
        //默认日志处理器
        if (StringUtils.isNotBlank(monitorLogConfiguration.getDefaultHandler())) {
            ILogHandler handler = (ILogHandler)applicationContext.getBean(monitorLogConfiguration.getDefaultHandler());
            if (handler == null) {
                logger.error("[日志处理器工厂] 初始化失败:默认日志处理器[{}]加载失败.", monitorLogConfiguration.getDefaultHandler());
                throw new BaseException("日志处理工厂初始化失败:默认日志处理器加载失败.");
            }
            defaultHandler = handler;
            logger.info("[日志处理器工厂] 成功设置默认日志处理器:{}", monitorLogConfiguration.getDefaultHandler());
        } else {
            logger.warn("[日志处理器工厂] 默认日志处理器为空,new一个BasicLogHandler!");
            defaultHandler = new BasicLogHandler();
        }
        //初始化处理器池
        String[] handlers = monitorLogConfiguration.getLogHandlerBeanNames();
        if (handlers != null && handlers.length > 0) {
            for (String handlerName : handlers) {
                ILogHandler handler = (ILogHandler)applicationContext.getBean(handlerName);
                if (handler != null) {
                    handlerPool.put(handlerName, handler);
                } else {
                    logger.error("[日志处理器工厂] 初始化失败:日志处理器[{}]加载失败.", handlerName);
                    throw new BaseException("日志处理工厂初始化失败:日志处理器["+handlerName+"]加载失败.");
                }
            }
        } else {
            logger.info("[日志处理器工厂] 日志处理器配置为空,所有的日志将使用默认处理器进行处理!");
        }
        logger.info("[日志处理器工厂] 初始化结束!");
    }
}
